{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2023 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# balance_min_flow"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/graph/balance_min_flow.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/graph/samples/balance_min_flow.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Assignment with teams of workers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.graph.python import min_cost_flow\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"Solving an Assignment with teams of worker.\"\"\"\n",
    "    smcf = min_cost_flow.SimpleMinCostFlow()\n",
    "\n",
    "    # Define the directed graph for the flow.\n",
    "    team_a = [1, 3, 5]\n",
    "    team_b = [2, 4, 6]\n",
    "\n",
    "    start_nodes = (\n",
    "        # fmt: off\n",
    "      [0, 0]\n",
    "      + [11, 11, 11]\n",
    "      + [12, 12, 12]\n",
    "      + [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6]\n",
    "      + [7, 8, 9, 10]\n",
    "        # fmt: on\n",
    "    )\n",
    "    end_nodes = (\n",
    "        # fmt: off\n",
    "      [11, 12]\n",
    "      + team_a\n",
    "      + team_b\n",
    "      + [7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10]\n",
    "      + [13, 13, 13, 13]\n",
    "        # fmt: on\n",
    "    )\n",
    "    capacities = (\n",
    "        # fmt: off\n",
    "      [2, 2]\n",
    "      + [1, 1, 1]\n",
    "      + [1, 1, 1]\n",
    "      + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n",
    "      + [1, 1, 1, 1]\n",
    "        # fmt: on\n",
    "    )\n",
    "    costs = (\n",
    "        # fmt: off\n",
    "      [0, 0]\n",
    "      + [0, 0, 0]\n",
    "      + [0, 0, 0]\n",
    "      + [90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, 60, 105, 80, 75, 45, 65, 110, 95]\n",
    "      + [0, 0, 0, 0]\n",
    "        # fmt: on\n",
    "    )\n",
    "\n",
    "    source = 0\n",
    "    sink = 13\n",
    "    tasks = 4\n",
    "    # Define an array of supplies at each node.\n",
    "    supplies = [tasks, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -tasks]\n",
    "\n",
    "    # Add each arc.\n",
    "    for i in range(0, len(start_nodes)):\n",
    "        smcf.add_arc_with_capacity_and_unit_cost(\n",
    "            start_nodes[i], end_nodes[i], capacities[i], costs[i]\n",
    "        )\n",
    "\n",
    "    # Add node supplies.\n",
    "    for i in range(0, len(supplies)):\n",
    "        smcf.set_node_supply(i, supplies[i])\n",
    "\n",
    "    # Find the minimum cost flow between node 0 and node 10.\n",
    "    status = smcf.solve()\n",
    "\n",
    "    if status == smcf.OPTIMAL:\n",
    "        print(\"Total cost = \", smcf.optimal_cost())\n",
    "        print()\n",
    "        for arc in range(smcf.num_arcs()):\n",
    "            # Can ignore arcs leading out of source or intermediate, or into sink.\n",
    "            if (\n",
    "                smcf.tail(arc) != source\n",
    "                and smcf.tail(arc) != 11\n",
    "                and smcf.tail(arc) != 12\n",
    "                and smcf.head(arc) != sink\n",
    "            ):\n",
    "                # Arcs in the solution will have a flow value of 1.\n",
    "                # There start and end nodes give an assignment of worker to task.\n",
    "                if smcf.flow(arc) > 0:\n",
    "                    print(\n",
    "                        \"Worker %d assigned to task %d.  Cost = %d\"\n",
    "                        % (smcf.tail(arc), smcf.head(arc), smcf.unit_cost(arc))\n",
    "                    )\n",
    "    else:\n",
    "        print(\"There was an issue with the min cost flow input.\")\n",
    "        print(f\"Status: {status}\")\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
